<?php

namespace App\Exports;

use App\Models\Record;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;

class RecordsExport implements FromQuery
{
    use Exportable;

    public function __construct(int $id)
    {
        $this->id = $id;
    }


    public function query()
    {
//        有id根据id查询，没有id查询全部，使用when方法
        return Record::query()->when($this->id, function ($query) {
            return $query->where("user_id", "=", $this->id);
        });
    }
}
